package com.ironman.original;

import cn.hutool.core.util.StrUtil;
import com.ironman.event.properties.SysTrainingProperties;
import com.ironman.original.generator.OriginalDataGenerator;
import lombok.extern.slf4j.Slf4j;
import org.dromara.mica.mqtt.codec.MqttQoS;
import org.dromara.mica.mqtt.core.client.MqttClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Slf4j
@Component
@ConditionalOnProperty(prefix = "sailing.original", name = "enable", havingValue = "true")
public class OriginalDataFactory {

    @Resource
    MqttClient mqttClient;

    @Resource
    SysTrainingProperties sysTrainingProperties;

    @Resource
    List<OriginalDataGenerator> originalDataGenerators;

    @PostConstruct
    public void init() {
        String topic = StrUtil.format("mobile/up/original/{}/{}/{}", sysTrainingProperties.getTenantCode(), sysTrainingProperties.getLevelId(), sysTrainingProperties.getMHmId());
        for (OriginalDataGenerator originalDataGenerator : originalDataGenerators) {
            originalDataGenerator.start(sysTrainingProperties.getMHmId(), originalData -> mqttClient.publish(topic, originalData.getBytes(StandardCharsets.UTF_8), MqttQoS.valueOf(1)));
        }
    }

}
